import pygame

# 初始化 Pygame
pygame.init()

# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("移动方块")

# 方块属性
square_size = 50
square_x = 375
square_y = 275
square_speed = 5

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 获取按键状态
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and square_x > 0:
        square_x -= square_speed
    if keys[pygame.K_RIGHT] and square_x < screen_width - square_size:
        square_x += square_speed
    if keys[pygame.K_UP] and square_y > 0:
        square_y -= square_speed
    if keys[pygame.K_DOWN] and square_y < screen_height - square_size:
        square_y += square_speed

    # 填充屏幕背景色
    screen.fill((255, 255, 255))

    # 绘制方块
    pygame.draw.rect(screen, (0, 255, 0), (square_x, square_y, square_size, square_size))

    # 更新屏幕显示
    pygame.display.flip()

# 退出 Pygame
pygame.quit()
    